import 'package:shared_preferences/shared_preferences.dart';

class LocalSharedCache {

  static String _expire_prefix = 'expire.';

  static Future<String?> getString(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    int? expireAt= sp.getInt('$_expire_prefix$key');
    if(expireAt != null ){
      return expireAt>DateTime.now().millisecond ? sp.getString(key) : null;
    }
    return sp.getString(key);
  }

  static Future<void> removeString(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.remove('$_expire_prefix$key');
    sp.remove(key);
  }

  static Future<void> saveString(String key, String value,{int expireMills=0}) async {
    ArgumentError.checkNotNull(key);
    SharedPreferences sp = await SharedPreferences.getInstance();
    if(expireMills>0){
      sp.setInt('$_expire_prefix$key', expireMills+DateTime.now().millisecond);
    }
    sp.setString(key, value);
  }

}
